package com.rui.dao.impl;

import com.rui.dao.StudentDao;
import com.rui.entity.Student;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;

/**
 * Created by Administrator on 2017/4/6.
 */
@Repository
public class StudentDAOImpl  implements StudentDao {

    @Autowired
    private SessionFactory sessionFactory;

    private Session getSession(){
        return sessionFactory.getCurrentSession();
    }

    public void add(Student student) {
        getSession().beginTransaction();
        getSession().save(student);
        getSession().getTransaction().commit();
    }

    public void delete(Integer id) {
        getSession().beginTransaction();
        Student student = (Student) getSession().get(Student.class, Integer.valueOf(id));
        if (student != null) {
            getSession().delete(student);
        }
        getSession().getTransaction().commit();
    }

    public void update(Student student) {
        getSession().beginTransaction();
        getSession().update(student);
        getSession().getTransaction().commit();
    }

    public Student findOne(Integer id) {
        getSession().beginTransaction();
        Student student = (Student) getSession().get(Student.class, Integer.valueOf(id));
        getSession().getTransaction().commit();
        return student;
    }

    public List<Student> findStudents(String realName, Integer pageSize, Integer pageIndex) {
        getSession().beginTransaction();
        StringBuilder sb = new StringBuilder("from Student where 1 = 1");
        if (StringUtils.isNotBlank(realName)) {
            sb.append(" and realName like '%" + realName + "%'");
        }
        Query query = getSession().createQuery(sb.toString());
        query.setFirstResult((pageIndex - 1) * pageSize);
        query.setMaxResults(pageSize);
        List<Student> students = query.list();
        getSession().getTransaction().commit();
        return students;
    }

    public Integer findTotalPage(String realName, Integer pageSize) {
        getSession().beginTransaction();
        StringBuilder sb = new StringBuilder("select count(id) from Student where 1 = 1");
        if (StringUtils.isNotBlank(realName)) {
            sb.append(" and realName like '%" + realName + "%'");
        }
        Query query = getSession().createQuery(sb.toString());
        Long count = (Long) query.uniqueResult();
        getSession().getTransaction().commit();
        if (count % pageSize == 0) {
            return count.intValue() / pageSize;
        } else {
            return count.intValue() / pageSize + 1;
        }
    }

    public Student findByUserName(String userName) {
        getSession().beginTransaction();
        StringBuilder sb = new StringBuilder("from Student where userName = '" + userName + "'");
        Query query = getSession().createQuery(sb.toString());
        List<Student> students = query.list();
        getSession().getTransaction().commit();
        if (students.size() > 0) {
            return students.get(0);
        } else {
            return null;
        }
    }

    public List<Student> findStudents(String userName, Integer id) {
        getSession().beginTransaction();
        StringBuilder sb = new StringBuilder("from Student where 1 = 1");
        if (StringUtils.isNotBlank(userName)) {
            sb.append(" and userName = '" + userName + "'");
        }
        if (id != null) {
            sb.append(" and id != " + id);
        }
        Query query = getSession().createQuery(sb.toString());
        List<Student> students = query.list();
        getSession().getTransaction().commit();
        return students;
    }
}
